const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
//记录文件数量
var fileNum = 0;

//设置唯一id
var fileId = 0;
Page({
  data: {
    title: "",
    content: "",
    //提交的数据
    param: {},
    reportFile: "",
    reportTitle: "",
    reportContent: "",
    //本地路径
    myJobList: [

    ],
    //服务器路径
    myFile: [],
    clickIndex: -1,
    selectIndex: -1,
    showMask: false,
    newTitle: ''
  },
  getReportTitle(e) {
    this.setData({
      reportTitle: e.detail.value,
    })
  },
  getReportContent(e) {
    this.setData({
      reportContent: e.detail.value,
    })
    // console.log(this.data.reportContent)
  },
  onLoad(options) {
    
    this.data.param = JSON.parse(options.param)
    // console.log(JSON.parse(options.param))
    wx.setNavigationBarTitle({
      title: '网上举报',
    })
  },

  //上传文件到本地
  chooseImage: function (e) {
    var that = this;
    if (fileNum < 5) {
      wx.chooseMessageFile({
        count: 1,
        success(res) {
          console.log(res);
          let tempFilePaths = res.tempFiles[0].path
          let name = res.tempFiles[0].name;
          // fileId++;
          
          // console.log(that.data.myJobList);
          wx.showLoading({
            title: '上传中..',
            icon: 'none'
          })
          //文件上传
          wx.uploadFile({
            url: api.uploadFile,
            name: 'file',
            filePath: tempFilePaths,
            header: {
              "Content-Type": "multipart/form-data",
            },
            formData: {
              file: tempFilePaths,
              uploadName: name
            },
            success: function (res) {
              console.log(res);
              let myJobListObj = {}
              myJobListObj.fileName = name
              myJobListObj.filePath = tempFilePaths
              that.data.myJobList.push(myJobListObj)

              let File = JSON.parse(res.data).data
              console.log(File);
              let myFileObj = {};
              myFileObj.filePath = File.file_path;
              myFileObj.fileName = File.file_name;
              
              that.data.myFile.push(myFileObj)
              that.setData({
                myFile: that.data.myFile,
                myJobList: that.data.myJobList
              })
              wx.showToast({
                title: '上传成功',
                icon: 'success',
                duration: 1000,
                success: () => {
                  //文件上传次数
                  fileNum++;
                },
              })
            },
            fail: function (res) {
              console.log(res)
            },
          })
        }
      })
    } else {
      wx.showToast({
        title: '最多上传5个文件',
        icon: 'none',
        duration: 1000
      })

    }

  },
  submit() {
    if (util.verifyEmpty(this.data.reportTitle, "标题")) {
      return false
    } else if (util.verifyEmpty(this.data.reportContent, "描述内容")) {
      return false
    } else {
      var param = this.data.param
      param.reportTitle = this.data.reportTitle
      param.reportContent = this.data.reportContent
      
      param.fileVoList = this.data.myFile
      
      util.request(api.ReportSave, param, 'POST', 'application/json').then(res => {
        console.log(res)
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 1500
        }).then(res => {
          fileNum = 0;
          wx.reLaunch({
            url: '/pages/report/report_list/report_list',
          })
        }).catch(err => console.log(err));
      });
    }
  },
  openDoc(e) {
    let i = e.currentTarget.dataset.index;
    let filePath = this.data.myJobList[i].filePath

    console.log(e);
    //打开文件
    wx.openDocument({
      filePath,
      success: function (res) {
        // console.log(res);
      },
      fail(err) {
        console.log(err);
      }
    })
  },
  // 删除
  del(e) {
    console.log(e);
    let that = this;
    let id = e.currentTarget.dataset.id;
    
    let myJobList = that.data.myJobList;
    let myFile = that.data.myFile;
    
    that.setData({
      clickIndex: id
    })
   
    wx.showModal({
      title: '提示',
      content: '是否确认删除',
      success(res) {
        
        //确定
        if (res.confirm) {
          myJobList.splice(that.data.clickIndex, 1);
          // myFile.splice(that.data.clickIndex, 1);
          //须在确定时用才能下次点击才不会失效 有返回值是就会执行
          let file_path = myFile.splice(that.data.clickIndex, 1)[0].path;
          fileNum--;
          that.setData({
            myJobList,
            myFile,
            clickIndex: -1
          })
          
          util.request(api.deleteByPath, {
            file_path
          }, 'GET').then(res => {
            console.log(res);
            wx.showToast({
              title: '删除成功',
              duration: 1000,
              icon: 'success'
            })
          })

        } else if (res.cancel) {
            that.setData({
              clickIndex: -1
            })
        }
      }
    })
  },
})